package com.project;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import com.google.gson.Gson;

@WebServlet("/ProcessModificationServlet")
public class ProcessModificationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 解析JSON数据
        String jsonData = request.getParameter("requestData");
        Map<String, String> requestData = new Gson().fromJson(jsonData, Map.class);
        String action = request.getParameter("action");
        
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null) {
            response.sendRedirect("login.jsp");
            return;
        }
        
        boolean result = true;
        List<String> input = new ArrayList<>();

        if (action.equals("approve")) {
            if("registration".equals(requestData.get("type"))) {
                input.add(requestData.get("name"));
                input.add(requestData.get("pid"));
                input.add(requestData.get("email"));
                input.add(requestData.get("phone"));
                input.add(requestData.get("password"));
                result = admin.Process_registration(input);
            } else {
                input.add(requestData.get("uid"));
                input.add(requestData.get("name"));
                input.add(requestData.get("pid"));
                input.add(requestData.get("email"));
                input.add(requestData.get("phone"));
                input.add(requestData.get("identity"));
                input.add(requestData.get("is_registered"));
                input.add(requestData.get("password"));
                switch (requestData.get("field")) {
                    case "name":
                        input.set(1, requestData.get("newValue"));
                        break;
                    case "email":
                        input.set(3, requestData.get("newValue"));
                        break;
                    case "phone":
                        input.set(4, requestData.get("newValue"));
                        break;
                    case "password":
                        input.set(7, requestData.get("newValue"));
                        break;
                    default:
                        response.getWriter().println("<html><body><h2>不支持修改该字段，请重试。</h2></body></html>");
                        return;
                }
                result = admin.ProcessUserModification(input);
            }
        }

        if (result) {
            // 从ServletContext中移除已处理的请求
            List<Map<String, String>> pendingRequests = (List<Map<String, String>>) request.getServletContext().getAttribute("pendingRequests");
            if (pendingRequests != null) {
                pendingRequests.removeIf(req -> req.equals(requestData));
                request.getServletContext().setAttribute("pendingRequests", pendingRequests);
            }
            response.sendRedirect("HandlingUsers.jsp?result=success");
        } else {
            response.sendRedirect("HandlingUsers.jsp?result=failure");
        }
    }
}